package com.jgp.config;

import com.jgp.attachment.props.AttachmentConfigPropBean;
import com.jgp.common.utils.JGPUtil;
import com.jgp.sys.ui.annotation.handler.UIParamHandler;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 项目   sys 装饰器
 * 作者   loufei
 * 时间   2018/2/24
 */
@Order(400)
@Configuration
@EnableConfigurationProperties(AttachmentConfigPropBean.class)
@ComponentScan(basePackages = {"com.**.controller"}, basePackageClasses = {Controller.class, RestController.class})
public class WebConfiguration implements WebMvcConfigurer {
    
    public static List<HttpMessageConverter<?>> CONVERTERS = Collections.emptyList();
    
    @Autowired
    private AttachmentConfigPropBean attachmentConfigPropBean;
    
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        //设置允许跨域的路径
        registry.addMapping("/**")
                //设置允许跨域请求的域名
                .allowedOrigins("*")
                //是否允许证书 不再默认开启
                .allowCredentials(true)
                //设置允许的方法
                .allowedMethods("*")
                //跨域允许时间
                .maxAge(3600);
    }
    
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        for (HttpMessageConverter converter : converters) {
            
            if (converter instanceof MappingJackson2HttpMessageConverter) {
                
                MappingJackson2HttpMessageConverter jacksonConverter =
                        
                        ((MappingJackson2HttpMessageConverter) converter);
                
                jacksonConverter.setObjectMapper(JGPUtil.MAPPER);
                
            }
            
        }
        CONVERTERS = converters;
    }
    
    @Override
    public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
        configurer.favorPathExtension(true).favorParameter(false).parameterName("mediaType").ignoreAcceptHeader(false)
                  .defaultContentType(MediaType.APPLICATION_JSON_UTF8).mediaType("xml", MediaType.APPLICATION_XML)
                  .mediaType("json", MediaType.APPLICATION_JSON_UTF8);
    }
    
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        
        List<String> from = new ArrayList();
        from.add("/**/*.txt");
        from.add("/**/*.html");
        from.add("/**/*.html");
        from.add("/webjars/**");
        from.add("/framework/**");
        from.add("/public/**");
        from.add("/static/**");
        from.add("/workflow/**");
        
        
        List<String> to = new ArrayList();
        to.add("classpath:/public/");
        to.add("classpath:/public/");
        to.add("classpath:/META-INF/resources/");
        to.add("classpath:/META-INF/resources/webjars/");
        to.add("classpath:/framework/");
        to.add("classpath:/public/");
        to.add("classpath:/static/");
        to.add("classpath:/back/workflow/");
        
        if (StringUtils.isNotBlank(attachmentConfigPropBean.getLocalDir())) {
            from.add("/file/**");
            String localDir = attachmentConfigPropBean.getLocalDir();
            to.add("file:" + (localDir.endsWith(File.separator) ? localDir : localDir + File.separator));
        }
        registry.addResourceHandler(from.toArray(new String[from.size()])).addResourceLocations(to.toArray(new String[to.size()]));
    }
    
    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
        argumentResolvers.add(new UIParamHandler(CONVERTERS));
    }
    
}
